﻿using DTcms.Common;
using System;
using System.Text;

namespace DTcms.Web.api.payment.paypal
{
    public partial class ipn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var param = Request.BinaryRead(Request.ContentLength);
            var strRequest = Encoding.ASCII.GetString(param);
            var vildResult = new API.Payment.paypal.PayPalNotice().NoticeVild(strRequest);
            if (!vildResult.Equals("VERIFIED"))
            {
                return;
            }

            var custom = DTRequest.GetFormString("custom"); //订单号
            var payment_gross = DTRequest.GetFormString("payment_gross"); //产品总价
            var txn_id = DTRequest.GetFormString("txn_id"); //PayPal交易Id

            var order_no = custom;

            if (order_no.StartsWith("R")) //充值订单
            {
                BLL.user_recharge bll = new BLL.user_recharge();
                Model.user_recharge model = bll.GetModel(order_no);
                if (model == null)
                {
                    Response.Write("该订单号不存在");
                    return;
                }
                if (model.status == 1) //已成功
                {
                    Response.Write("success");
                    return;
                }
                if (model.amount != (decimal.Parse(payment_gross) / 100))
                {
                    Response.Write("订单金额和支付金额不相符");
                    return;
                }
                bool result = bll.Confirm(order_no);
                if (!result)
                {
                    Response.Write("修改订单状态失败");
                    return;
                }
            }
            else if (order_no.StartsWith("B")) //商品订单
            {
                BLL.orders bll = new BLL.orders();
                Model.orders model = bll.GetModel(order_no);
                if (model == null)
                {
                    Response.Write("该订单号不存在");
                    return;
                }
                if (model.payment_status == 2) //已付款
                {
                    Response.Write("success");
                    return;
                }
                if (model.order_amount != decimal.Parse(payment_gross))
                {
                    Response.Write("订单金额和支付金额不相符");
                    return;
                }
                bool result = bll.UpdateField(order_no, "trade_no='" + txn_id + "',status=2,payment_status=2,payment_time='" + DateTime.Now + "'");
                if (!result)
                {
                    Response.Write("修改订单状态失败");
                    return;
                }
                //扣除积分
                if (model.point < 0)
                {
                    new BLL.user_point_log().Add(model.user_id, model.user_name, model.point, "换购扣除积分，订单号：" + model.order_no, false);
                }
            }

        }
    }
}

//mc_gross=300.00&
//protection_eligibility=Eligible&
//address_status=confirmed&
//item_number1=&
//payer_id=SHCLCZM9C8PAJ&
//tax=0.00&
//address_street=1 Main St&
//payment_date=18:45:42 Mar 01, 2021 PST&
//payment_status=Completed&
//charset=windows-1252&
//address_zip=95131&
//mc_shipping=0.00&
//mc_handling=0.00&
//first_name=John&
//mc_fee=9.00&
//address_country_code=US&
//address_name=John Doe&
//notify_version=3.9&
//custom=&
//payer_status=verified&
//business=sb-jzhci1772567@business.example.com&
//address_country=United States&
//num_cart_items=1&
//address_city=San Jose&
//verify_sign=A39FCPOSUnh-g9pqRg2HQNbzKvI6AWjAeO6WG2GY.p5scpTP221H-IcC&
//payer_email=sb-bcbws1806072@personal.example.com&
//tax1=0.00&
//txn_id=6V839327W6264873F&
//payment_type=instant&
//last_name=Doe&
//address_state=CA&
//item_name1=This Is A Book&
//receiver_email=sb-jzhci1772567@business.example.com&
//payment_fee=9.00&
//shipping_discount=0.00&
//quantity1=2&
//insurance_amount=0.00&
//receiver_id=5JXGDSWF5MP5L&
//txn_type=cart&
//discount=0.00&
//mc_gross_1=300.00&
//mc_currency=USD&
//residence_country=US&
//test_ipn=1&
//shipping_method=Default&
//transaction_subject=&
//payment_gross=300.00&
//ipn_track_id=de7e979c6619e